﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace eVoting
{
    public partial class VotingResults : System.Web.UI.Page
    {
        DBManager db;
        int userId = 3;
        int selectedPoll;
        protected void Page_Load(object sender, EventArgs e)
        {

            db = new DBManager();
            if (!IsPostBack)
            {
                List<Poll> polls = db.getUserTerminatedPolls(userId);

                PollsDdl.Items.Add("Select a poll");
                
                for (int i = 0; i < polls.Count; i++)
                {
                    PollsDdl.Items.Add(new ListItem(polls[i].name, polls[i].ID + ""));

                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PollsDdl.SelectedIndex != 0)
            {
                selectedPoll = int.Parse(PollsDdl.SelectedItem.Value);

                List<Candidate> candidatesResult = db.getPollResults(selectedPoll);
                int numberOfVoters = 0;

                for (int i = 0; i < candidatesResult.Count; i++)
                {
                    numberOfVoters += candidatesResult[i].voteCount;
                }

                for (int i = 0; i < candidatesResult.Count; i++)
                {
                    TableRow row = new TableRow();
                    TableCell cell1 = new TableCell();
                    cell1.Text = candidatesResult[i].firstName + " " + candidatesResult[i].middleName + " " + candidatesResult[i].lastName;
                    row.Cells.Add(cell1);
                    TableCell cell2 = new TableCell();
                    double votesPercentage = candidatesResult[i].voteCount * 100 / numberOfVoters;
                    
                    Image img = new Image();
                    img.ImageUrl = "~/images/filledbar.png";
                    img.Style["width"] = votesPercentage * 2 + "%"; 
                    img.Style["height"] =  "15px";
                    cell2.Controls.Add(img);

                    Label cell2Text = new Label();
                    cell2Text.Text = "\n" + votesPercentage + "% (" +candidatesResult[i].voteCount + " votes)";
                    cell2.Controls.Add(cell2Text);

                    row.Cells.Add(cell2);
                    ResultTable.Rows.Add(row);
                    

                }
                NumberOfVotersLbl.Text = "Total number of votes: " + numberOfVoters; 
            }
            else
            {
                NumberOfVotersLbl.Text = "";
             //   ResultTable.Dispose();
            }
            

        }

    }
}
